package com.hundredapp.oa.web.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.hundredapp.oa.dao.SelectType;
import com.hundredapp.oa.domain.OaReserve;
import com.hundredapp.oa.repository.ReserveManager;
import com.hundredapp.oa.repository.SecuitManager;
import com.hundredapp.oa.repository.UserManager;
import com.hundredapp.oa.util.JSEscape;
import com.hundredapp.oa.util.WebUtil;
import com.hundredapp.oa.vo.ReserveVO;
import com.opensymphony.xwork2.ActionSupport;

@Repository
@Scope("prototype")
public class Reserve extends ActionSupport {

	private List<ReserveVO> root;
	
	private Object success;
	private String msg;
	private int id;
	private int start;
	private int limit;
	private int total;
	private int secuitid;
	private int userid;
	private OaReserve reserve;
	private String searchstudent;
	private String searchteacher;
	private ReserveVO reservevo;
	
	@Autowired
	private ReserveManager reserveManager;
	@Autowired
	private UserManager userManager;
	@Autowired
	private SecuitManager secuitManager;
	
	public String add() {
		try{
			reserve.setOaSecuit(secuitManager.findSecuitById(secuitid));
			reserve.setOaUser(userManager.findUserById(userid));
			reserve.setCreateUserId(WebUtil.GetLoginUserId());
			reserveManager.add(reserve);
			success=true;
			msg="添加成功";
		}catch(Exception e) {
			e.printStackTrace();
			success=false;
			msg="添加失败";
		}
		return SUCCESS;
	}
	
	public String allReserve() {
		String condition = SelectType.SELECT_ALL;
		if(searchstudent != null || searchteacher != null) {
			searchstudent = JSEscape.unescape(searchstudent);
			searchteacher = JSEscape.unescape(searchteacher);
			if(searchstudent!=null)
				condition += " and oaSecuit.displayName like '%" +searchstudent+"%' ";
			if(searchteacher!=null)
				condition += " and oaUser.displayName like '%" +searchteacher+"%' ";
		}
		root=new ArrayList<ReserveVO>();
		for(OaReserve or:reserveManager.findAllReserve(condition, start, limit)){
			ReserveVO temp=new ReserveVO();
			temp.setId(or.getId());
			temp.setPhone(or.getPhone());
			temp.setReserveTime(or.getReserveTime());
			temp.setSecuitName(or.getOaSecuit().getDisplayName());
			temp.setUserName(or.getOaUser().getDisplayName());
			root.add(temp);
		}
		total=reserveManager.count(condition);
		return SUCCESS;
	}
	
	public String delete() {
		try{
			reserveManager.delete(id);
			success=true;
			msg="删除成功";
		}catch(Exception e) {
			e.printStackTrace();
			success=false;
			msg="删除失败";
		}
		return SUCCESS;
	}
	
	public String find() {
		OaReserve reserve=reserveManager.findReserveById(id);
		reservevo = new ReserveVO();
		reservevo.setId(reserve.getId());
		reservevo.setPhone(reserve.getPhone());
		reservevo.setReserveTime(reserve.getReserveTime());
		reservevo.setSecuitName(reserve.getOaSecuit().getDisplayName());
		reservevo.setUserName(reserve.getOaUser().getDisplayName());
		return SUCCESS;
	}
	
	public List<ReserveVO> getRoot() {
		return root;
	}
	public Object getSuccess() {
		return success;
	}
	public String getMsg() {
		return msg;
	}
	public void setId(int id) {
		this.id = id;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	public void setTotal(int total) {
		this.total = total;
	}

	public int getSecuitid() {
		return secuitid;
	}

	public void setSecuitid(int secuitid) {
		this.secuitid = secuitid;
	}

	public int getUserid() {
		return userid;
	}

	public void setUserid(int userid) {
		this.userid = userid;
	}

	public int getId() {
		return id;
	}

	public int getStart() {
		return start;
	}

	public int getLimit() {
		return limit;
	}

	public int getTotal() {
		return total;
	}

	public void setRoot(List<ReserveVO> root) {
		this.root = root;
	}

	public void setSuccess(Object success) {
		this.success = success;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public ReserveVO getReservevo() {
		return reservevo;
	}

	public void setReservevo(ReserveVO reservevo) {
		this.reservevo = reservevo;
	}

	public String getSearchstudent() {
		return searchstudent;
	}

	public void setSearchstudent(String searchstudent) {
		this.searchstudent = searchstudent;
	}

	public String getSearchteacher() {
		return searchteacher;
	}

	public void setSearchteacher(String searchteacher) {
		this.searchteacher = searchteacher;
	}

	public OaReserve getReserve() {
		return reserve;
	}

	public void setReserve(OaReserve reserve) {
		this.reserve = reserve;
	}
	
}
